package com.ztesoft.report.utils;

import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author :xie
 * Email: 1487471733@qq.com
 * Date: 2018/12/10
 * Time: 18:12
 * Describe:list需要有序，画多边形 的最后一步是第一个点与最后一个点相连接
 */
public class GeneralPathUtil {

    public static boolean isIns(double pointX, double pointY, Map map){
        //{起始行，结束行，起始列，结束列}
        List<Integer>  firstRows=(List<Integer>) map.get("firstRows");
        List<Integer>  lastRows=(List<Integer>) map.get("lastRows");
        List<Integer>  firstColumns= (List<Integer>) map.get("firstColumns");
        List<Integer>  lastColumns=(List<Integer>) map.get("lastColumns");

        for (int i=0;i<firstColumns.size();i++){

            //如何判断是否是一条线  起始行==结束行
            if (firstRows.get(i).equals(lastRows.get(i))&&Double.parseDouble(firstRows.get(i).toString())==pointY){
                if (Double.parseDouble(firstColumns.get(i).toString())<=pointX&&Double.parseDouble(lastColumns.get(i).toString())>=pointX){
                    return true;
                }
            }else{
                double[] Xs={firstColumns.get(i),lastColumns.get(i),lastColumns.get(i),firstColumns.get(i)};
                double[] Ys={firstRows.get(i),firstRows.get(i),lastRows.get(i),lastRows.get(i)};
                boolean inPolygon = isInPolygon(pointX, pointY, Xs, Ys);
                if (inPolygon){
                    return true;
                }
            }

        }

        return false;

    }

    /**
     * 判断是否在多边形区域内
     *
     * @param pointX
     *            要判断的点的横坐标
     * @param pointY
     *            要判断的点的纵坐标
     * @param Xs
     *            区域各顶点的横坐标数组
     * @param Ys
     *            区域各顶点的纵坐标数组
     * @return
     */
    public static boolean isInPolygon(double pointX, double pointY, double[] Xs,
                                      double[] Ys) {
        // 将要判断的横纵坐标组成一个点
        Point2D.Double point = new Point2D.Double(pointX, pointY);
        // 将区域各顶点的横纵坐标放到一个点集合里面
        List<Point2D.Double> pointList = new ArrayList<Point2D.Double>();
        double polygonPoint_x = 0.0, polygonPoint_y = 0.0;
        for (int i = 0; i < Xs.length; i++) {
            polygonPoint_x = Xs[i];
            polygonPoint_y = Ys[i];
            Point2D.Double polygonPoint = new Point2D.Double(polygonPoint_x, polygonPoint_y);
            pointList.add(polygonPoint);
        }
        return check(point, pointList);
    }
    /**
     * 一个点是否在多边形内
     *
     * @param point
     *            要判断的点的横纵坐标
     * @param polygon
     *            组成的顶点坐标集合
     * @return
     */
    private static boolean check(Point2D.Double point, List<Point2D.Double> polygon) {
        java.awt.geom.GeneralPath peneralPath = new java.awt.geom.GeneralPath();

        Point2D.Double first = polygon.get(0);
        // 通过移动到指定坐标（以双精度指定），将一个点添加到路径中
        peneralPath.moveTo(first.x, first.y);
        polygon.remove(0);
        for (Point2D.Double d : polygon) {
            // 通过绘制一条从当前坐标到新指定坐标（以双精度指定）的直线，将一个点添加到路径中。
            peneralPath.lineTo(d.x, d.y);
        }
        // 将几何多边形封闭
        peneralPath.lineTo(first.x, first.y);
        peneralPath.closePath();
        // 测试指定的 Point2D 是否在 Shape 的边界内。
        return peneralPath.contains(point);
    }
}
